<template>
	<el-container v-if="!dialog.save&&!dialog.detail">	
		<el-header>
			<div class="left-panel">
				<div class="module-filter">
					<label style="margin-right: 10px">{{$t('theatre_data.userKeyworkLabel')}}</label>
					<div class="el-select el-select--default">
						<el-input type="text" v-model="params.searchKeywords" :placeholder="$t('table_data.keyWordText')" clearable></el-input>
					</div>
				</div>
				<div class="module-filter">
					<el-button type="primary" icon="el-icon-search" @click="upsearch">{{ $t('table_data.searchText') }}</el-button>
					<el-button @click="reset">{{ $t('table_data.resetText') }}</el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<!-- <div class="top-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add">{{ $t('table_data.addText') }}</el-button>
			</div> -->
			<scTable ref="table" :apiObj="apiObj" row-key="id" stripe border :params="params" :hideSetting="true" @sortChange="sortChange">
				<el-table-column :label="$t('theatre_data.theatre_name')" prop="theatreName" width="auto"></el-table-column>
				<!-- <el-table-column :label="$t('theatre_data.area')" prop="area" width="auto"></el-table-column> -->
				<el-table-column :label="$t('brand_data.form.zipCode')" prop="longitude" width="auto">
					<template #default="scope">
						<span>{{ scope.row.postalCode + '-' + scope.row.postalNo }}</span>
					</template>
				</el-table-column>
				<!-- <el-table-column :label="$t('theatre_data.longitude_dimension')" prop="longitude" width="auto">
					<template #default="scope">
						<span>{{ scope.row.longitude + '-' + scope.row.latitude }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('user_data.mineEmail')" prop="email" width="auto"></el-table-column> -->
				<el-table-column :label="$t('brand_data.address')" prop="officialAddress" width="auto"></el-table-column>
				<el-table-column :label="$t('brand_data.phone')" width="auto">
					<template #default="scope">
						<span>{{ scope.row.phoneCode + '-' + scope.row.phoneNo }}</span>
					</template>
				</el-table-column>
				<el-table-column :label="$t('order_data.status')" width="auto">
					<template #default="scope">
						{{ scope.row.status==1?$t('table_data.open'):$t('table_data.close') }}
					</template>
				</el-table-column>
				<el-table-column :label="$t('table_data.createAt')" prop="createAt" width="auto" sortable></el-table-column>
				<el-table-column :label="$t('table_data.operate')" width="auto" align="center">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_detail(scope.row, scope.$index)">{{ $t('table_data.detail') }}</el-button>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">{{ $t('table_data.edit') }}</el-button>
							<el-button text type="success" v-if="scope.row.status==2" size="small" @click="table_operate(scope.row, 1)">{{ $t('table_data.open') }}</el-button>
							<el-button text type="danger" v-if="scope.row.status==1" size="small" @click="table_operate(scope.row, 0)">{{ $t('table_data.close') }}</el-button>
							<!-- <el-popconfirm :title="$t('table_data.del_text')" @confirm="table_del(scope.row, scope.$index)">
								<template #reference>
									<el-button text type="primary" size="small">{{ $t('table_data.del') }}</el-button>
								</template>
							</el-popconfirm> -->
						</el-button-group>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
		<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
	import saveDialog from './client/save'

	export default {
		name: "client",
		components: {
			saveDialog
		},
		data(){
			return {
				langList: [
					{
						name: '简体中文',
						value: 'zh_CN',
					},
					{
						name: '繁體中文',
						value: 'zh_TW',
					},
					{
						name: 'English',
						value: 'en_US',
					},
					{
						name: '日本語',
						value: 'ja_JP',
					}
				],
				statusList: [
					{
						name: this.$t('user_data.status.type1'),
						value: 0,
					},
					{
						name: this.$t('user_data.status.type2'),
						value: 1,
					},
					{
						name: this.$t('user_data.status.type3'),
						value: 2,
					},
					{
						name: this.$t('user_data.status.type4'),
						value: 9,
					}
				],
				typeList: [
					{
						name: this.$t('user_data.userType.type1'),
						value: 1,
					},
					{
						name: this.$t('user_data.userType.type2'),
						value: 2,
					}
				],
				dialog: {
					save: false,
					detail: false
				},
				search: {
					keyword: ""
				},
				apiObj: {},
				selection: [],
				searchKey: null,
				params: {
					searchKeywords: null,
					status: null,
					userType: null,
					language: null,
					userName: this.searchKey,
					email: this.searchKey,
					phoneNo: this.searchKey,
					fullName: this.searchKey
				}
			}
		},
    async created(){
			this.apiObj = this.$API.theatre.getTheatre;
    },
		methods: {
			reset(){
				this.params.searchKeywords = null;
				this.params.status = null;
				this.params.userType = null;
				this.params.language = null;
				this.$refs.table.upData(this.params);
			},
			upsearch(){
				this.$refs.table.upData(this.params);
			},
			//增加
			add(){
				this.$router.push({path:'/theatreTable/client/save',query:{type:'add','id':'add'}})
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open()
				// })
			},
			//编辑
			table_edit(row){
				this.$router.push({path:'/theatreTable/client/save',query:{type:'edit','id':row.theatreId}})
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open('edit').setData(row)
				// })
			},
			// 详情
			table_detail(row){
				this.$router.push({path:'/theatreTable/client/save',query:{type:'detail','id':row.theatreId}})
				// this.dialog.save = true
				// this.$nextTick(() => {
				// 	this.$refs.saveDialog.open('edit').setData(row)
				// })
			},
			// 操作
			async table_operate(row, type){
				console.log(111111,type)
				this.$confirm(this.$t('table_data.optionTips',{type: type==1?this.$t('table_data.open'):this.$t('table_data.close')}), this.$t('table_data.tips'), {
					confirmButtonText: this.$t('table_data.editText'),
					cancelButtonText: this.$t('table_data.cancelText'),
					type: 'warning'
				}).then(async () => {
					var res = await this.$API.brand_theatre.theatreOperate.post(row.theatreId);
					if(res.code == 'OK'){
						//这里选择刷新整个表格 OR 插入/编辑现有表格数据
						this.$refs.table.upData(this.params);
						this.$message.success(this.$t('table_data.delsucc'))
					}else{
						this.$alert(res.message, this.$t('table_data.tips'), {type: 'error'})
					}
				}).catch(() => {
					this.$message({
						type: 'info',
						message: 'cancel'
					});          
				});
			},
			//批量删除
			async batch_del(){
				this.$confirm(`${this.$t('table_data.deltips1')} ${this.selection.length} ${this.$t('table_data.deltips2')}`, this.$t('table_data.tips'), {
					type: 'warning'
				}).then(() => {
					const loading = this.$loading();
					this.selection.forEach(item => {
						this.$refs.table.tableData.forEach((itemI, indexI) => {
							if (item.id === itemI.id) {
								this.$refs.table.tableData.splice(indexI, 1)
							}
						})
					})
					loading.close();
					this.$message.success(this.$t('table_data.operasucc'))
				}).catch(() => {

				})
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//本地更新数据
			handleSuccess(data, mode){
				if(mode=='add'){
					data.id = new Date().getTime()
					this.$refs.table.tableData.unshift(data)
				}else if(mode=='edit'){
					this.$refs.table.tableData.filter(item => item.id===data.id ).forEach(item => {
						Object.assign(item, data)
					})
				}
			}
		}
	}
</script>

<style scoped>
.module-filter {
	margin-right: 10px;
}
.el-header {
	--el-header-height: auto;
}
.left-panel {
	flex-wrap: wrap;
	line-height: 50px;
}
.top-panel {
	margin: 10px;
}
.scTable {
	height: calc(100% - 60px) !important;
}
.el-main.nopadding {
	margin: 0 10px;
}
.el-header {margin: 10px;border-radius: 4px;}
</style>
